//
//  PhotoViewCell.m
//  TestDemo
//
//  Created by apple on 16/5/2.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "PhotoViewCell.h"
#import "UIImageView+WebCache.h"

@interface PhotoViewCell ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *titleLbl;
@property (weak, nonatomic) IBOutlet UIButton *checkBoxBtn;

@end

@implementation PhotoViewCell

- (void)awakeFromNib {
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(beginEdit) name:beginEditNoticeName object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(cancelEdit) name:cancelEditNoticeName object:nil];
}

-(void)setPhoto:(PhotoModel *)photo{
    _photo = photo;
    if (photo.image) {
        self.imageView.image = photo.image;
    }else{
        NSURL *url = [NSURL URLWithString:photo.url];
        __weak PhotoModel *weekPhoto = photo;
        [self.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeHolder"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if (image) {
                weekPhoto.image = image;
            }
        }];
    }
    self.titleLbl.text = photo.fileId;
}


- (IBAction)checkBtnClicked:(UIButton *)sender {
    sender.selected = !sender.selected;
    if ([self.delegate respondsToSelector:@selector(checkboxBtn:clickedAtIndexPath:)]) {
        [self.delegate checkboxBtn:sender clickedAtIndexPath:self.indexPath];
    }
}

-(void)unCheckForCheckbxo{
    self.checkBoxBtn.selected = NO;
}

#pragma mark - 通知方法
-(void)beginEdit{
    self.checkBoxBtn.hidden = NO;
}

-(void)cancelEdit{
    self.checkBoxBtn.hidden = YES;
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

@end
